package yf.platform.tag;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.yfsoft.comm.util.StringUtil;

public class YfFormTag extends SimpleTagSupport  {

	private Map<String,Object> model;
	
	private Map<String,Object> entry;
	
	private List<Map<String,Object>> controls;
	
	private String p;
	
	public String getP() {
		return p;
	}

	private boolean flag = false;
	
	public void setP(String p) {
		this.p = p;
		this.flag = "edit".equals(p);	
	}


	public Map<String, Object> getModel() {
		return model;
	}


	public void setModel(Map<String, Object> model) {
		this.model = model;
	}


	public Map<String, Object> getEntry() {
		return entry;
	}



	public void setEntry(Map<String, Object> entry) {
		this.entry = entry;
	}



	public List<Map<String, Object>> getControls() {
		return controls;
	}



	public void setControls(List<Map<String, Object>> controls) {
		this.controls = controls;
	}


	@Override
	public void doTag() throws JspException, IOException {
		StringBuilder sb = new StringBuilder();
		sb.append("<legend>&nbsp;&nbsp;&nbsp;");
		sb.append(flag?"<i class=\"icon-pencil\"></i>&nbsp;修改":"<i class=\"icon-plus\"></i>&nbsp;新建");
		HttpServletRequest request = (HttpServletRequest)((PageContext)this.getJspContext()).getRequest();
		sb.append(StringUtil.format("</legend><form class=\"form-horizontal {0}\" method=\"post\" id=\"{1}\" action=\"{2}/c?{3}&m_id={4}&o={5}&v={6}&isajax=1\" {7}>",
				model.get("m_attr_class"),model.get("m_attr_id"),request.getContextPath(),model.get("m_action"),model.get("m_id"),p,model.get("m_view"),model.get("m_attr_other")));
		//&v=${v}&pri=${pri}&key=${key}&isajax=<c:if test="${tm eq null }">1</c:if><c:if test="${tm ne null }">0</c:if>
		//2.如果是修改需要显示当前连接项
		String tag,val="",auto,edit,pri;
		
		
		//新建
		//1.需要显示默认值
		for(Map<String,Object> c : this.controls){
			Object temp_o;
			//修改
			if(flag){
				temp_o = entry.get(c.get("mc_attr_name"));
				if(null!=temp_o){
					val = temp_o.toString();
				}
			}else{
				temp_o= c.get("mc_dft_val");
				if(null!=temp_o){
					val = temp_o.toString();
				}
				if (val.startsWith("#")) {
					HttpSession session = (HttpSession)((PageContext)this.getJspContext()).getSession();
					val = val.substring(1);
					int pos = val.indexOf(".");
					//直接取数
					if (pos < 0) {
						val = session.getAttribute(val).toString();
					} else {
						//有二级
						val = ((Map<String, Object>) session.getAttribute(val.substring(0, pos))).get(val.substring(pos + 1)).toString();
					}
				}else if("time".equals(val)){
					//
					SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
					val = df.format(new Date());
				}else if("date".equals(val)){
					//
					SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
					val = df.format(new Date());
				}
			}
			
			tag = c.get("c_tag").toString();
			//非用户可见的字段信息
			if(tag.matches("(hidden)|(updatedate)|(createdate)")){
				sb.append(StringUtil.format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", c.get("mc_attr_name"),val));
				continue;
			}
			pri = c.get("mc_pri").toString().trim();
			//主键&修改
			if("1".equals(pri)&&flag){
				sb.append(StringUtil.format("<input type=\"hidden\" name=\"pri\" value=\"{0}\" />", c.get("mc_attr_name")));
				sb.append(StringUtil.format("<input type=\"hidden\" name=\"key\" value=\"{0}\" />", entry.get(c.get("mc_attr_name"))));
			}
			auto = c.get("mc_auto").toString().trim();
			//忽略自增字段
			if("1".equals(auto)){
				continue;
			}
			
			//忽略不可修改字段
			edit = c.get("mc_edit").toString().trim();
			if("0".equals(edit)){
				continue;
			}
			sb.append(StringUtil.format("<div class=\"control-group\"><label class=\"control-label\">{0}:</label><div class=\"controls\"><span class=\"{1}\">",
					c.get("mc_prompt"),c.get("c_attr_class")));
			
			if(tag.equals("textarea")){
				sb.append(StringUtil.format("<textarea name=\"{0}\" class=\"txtarea\">{1}</textarea>",c.get("mc_attr_name"),val));
			}else if(tag.matches("(password)|(text)")){
				sb.append(StringUtil.format("<input type=\"{0}\" name=\"{1}\"class=\"field\" value=\"{2}\" />",
						tag,c.get("mc_attr_name"),val));
			}else if(tag.equals("date")){
				sb.append(StringUtil.format("<input type=\"text\" name=\"{0}\"class=\"field date-picker\" value=\"{1}\" />",
						c.get("mc_attr_name"),val));
			}else if(tag.equals("ynradio")){
				sb.append(StringUtil.format("<input class=\"yncheckbox\" type=\"checkbox\" {0}/><input type=\"hidden\"  name=\"{1}\" class=\"ynradio\" value=\"{2}\" />",
						val.equals("1")?"checked='checked'":"",c.get("mc_attr_name"),val ));
			}else if(tag.equals("file")){
				sb.append(StringUtil.format("<input type=\"text\" class=\"file-input field\" name=\"{0}\" value=\"{1}\" />", c.get("mc_attr_name"),val))
				.append(StringUtil.format("<input type=\"hidden\" id=\"{0}-guid\" name=\"guid\" value=\"\"/>",  c.get("mc_attr_name")))
				.append(StringUtil.format("<input type=\"hidden\" id=\"{0}-ext\" name=\"file_type\" value=\"\"/>",  c.get("mc_attr_name")));
			}else if(tag.equals("select")){
				sb.append(StringUtil.format("<select name=\"{0}\" class=\"\">{1}</select>", c.get("mc_attr_name"),c.get("mc_ds")));
			}
			sb.append("</span></div></div>");
			
		}
		
		
		sb.append("<div class=\"form-actions\"><button class=\"btn btn-primary btn-ajax-save\" o=\"").append(p).append("\" isajax=\"1\">")
			.append("<i class=\"icon-save\"></i> <span>保存</span></button>")
			.append("<button class=\"btn btn-back\"><i class=\"icon-reply\"></i> <span>返回</span></button></div>");
		sb.append("</form>");
		getJspContext().getOut().write(sb.toString());
		
		super.doTag();
	}

}
